package com.cskaoyan.controller.backgroundsystemcontroller;

import com.cskaoyan.bean.bo.GoodsDeleteBo;
import com.cskaoyan.bean.bo.GoodsListBo;
import com.cskaoyan.bean.bo.GoodsUpdateBo;
import com.cskaoyan.bean.pojo.Goods;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.GoodsCatAndBrandVo;
import com.cskaoyan.bean.vo.GoodsDetailVo;
import com.cskaoyan.bean.vo.GoodsListVo;
import com.cskaoyan.exception.ParameterException;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.utils.InvalidatedParamsUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 杨丰韶
 * @class GoodsController
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("list")
    @RequiresPermissions(value = {"*","admin:goods:list"},logical = Logical.OR)
    public BaseRespVo<GoodsListVo> goodsList(GoodsListBo goodsParams) {
        GoodsListVo goodsListVo = goodsService.queryGoodsList(goodsParams);

        return BaseRespVo.ok(goodsListVo);

    }

    @GetMapping("catAndBrand")
    public BaseRespVo<GoodsCatAndBrandVo> catAndBrand() {
        GoodsCatAndBrandVo goodsCatAndBrandVo = goodsService.queryGoodsCatAndBrand();

        return BaseRespVo.ok(goodsCatAndBrandVo);
    }
    @RequiresPermissions(value = {"*","admin:goods:detail"},logical = Logical.OR)
    @GetMapping("detail")
    public BaseRespVo<GoodsDetailVo> goodsDetail(Integer id) {
        GoodsDetailVo goodsDetailVo = goodsService.queryGoodsDetail(id);
        return BaseRespVo.ok(goodsDetailVo);
    }

    @RequiresPermissions(value = {"*","admin:goods:update"},logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo goodsUpdate(@RequestBody @Validated GoodsUpdateBo goods, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            BaseRespVo baseRespVo = InvalidatedParamsUtil.resErrorVo(bindingResult);
            return baseRespVo;
        }
        Integer result = goodsService.changeGood(goods);
        if (result == 200) {
            return BaseRespVo.ok();
        }
        if (result == 500) {
            return BaseRespVo.fail("商品编号不能被修改");
        }
        if (result == 501) {
            return BaseRespVo.fail("请至少添加一种规格");
        }
        return BaseRespVo.fail("服务器正在升级维护，请稍后再试");

    }

    @PostMapping("create")
    @RequiresPermissions(value = {"*","admin:goods:create"},logical = Logical.OR)
    public BaseRespVo goodsCreate(@RequestBody @Validated GoodsUpdateBo goodsUpdate, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return InvalidatedParamsUtil.resErrorVo(bindingResult);
        }
        Integer result = goodsService.addGood(goodsUpdate);

        if (result == 200) {
            return BaseRespVo.ok();
        }
        if (result == 500) {

            return BaseRespVo.fail("商品编号已存在");
        }
        if (result == 501) {
            return BaseRespVo.fail("请选择商品分类信息");
        }
        if (result == 502) {
            return BaseRespVo.fail("请至少添加一个商品规格");
        }
        return BaseRespVo.fail();
    }

    @PostMapping("delete")
    @RequiresPermissions(value = {"*","admin:goods:delete"},logical = Logical.OR)
    public BaseRespVo goodsDelete(@RequestBody GoodsDeleteBo goodsDeleteBo) {
        Integer goodsId = goodsDeleteBo.getId();
        Integer affectedRows = goodsService.removeGoodsDetail(goodsId);

        if (affectedRows == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();

    }

}
